package com.ruoyi.web.controller.estate.emergency;

import java.util.List;
import java.util.Map;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.estate.emergency.EmergencySuppliesStock;
import com.ruoyi.system.domain.estate.emergency.EmergencySuppliesRecord;
import com.ruoyi.system.service.estate.emergency.IEmergencySuppliesStockService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 应急物资库存Controller
 */
@RestController
@RequestMapping("/estate/emergency/supplies/stock")
public class EmergencySuppliesStockController extends BaseController {
    @Autowired
    private IEmergencySuppliesStockService emergencySuppliesStockService;

    /**
     * 查询应急物资库存列表
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:stock:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmergencySuppliesStock emergencySuppliesStock) {
        startPage();
        List<EmergencySuppliesStock> list = emergencySuppliesStockService.selectEmergencySuppliesStockList(emergencySuppliesStock);
        return getDataTable(list);
    }

    /**
     * 导出应急物资库存列表
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:stock:export')")
    @Log(title = "应急物资库存", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EmergencySuppliesStock emergencySuppliesStock) {
        List<EmergencySuppliesStock> list = emergencySuppliesStockService.selectEmergencySuppliesStockList(emergencySuppliesStock);
        ExcelUtil<EmergencySuppliesStock> util = new ExcelUtil<EmergencySuppliesStock>(EmergencySuppliesStock.class);
        return util.exportExcel(list, "应急物资库存数据");
    }

    /**
     * 获取应急物资库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:stock:query')")
    @GetMapping(value = "/{stockId}")
    public AjaxResult getInfo(@PathVariable("stockId") Long stockId) {
        return success(emergencySuppliesStockService.selectEmergencySuppliesStockById(stockId));
    }

    /**
     * 新增应急物资库存
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:stock:add')")
    @Log(title = "应急物资库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmergencySuppliesStock emergencySuppliesStock) {
        return toAjax(emergencySuppliesStockService.insertEmergencySuppliesStock(emergencySuppliesStock));
    }

    /**
     * 修改应急物资库存
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:stock:edit')")
    @Log(title = "应急物资库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmergencySuppliesStock emergencySuppliesStock) {
        return toAjax(emergencySuppliesStockService.updateEmergencySuppliesStock(emergencySuppliesStock));
    }

    /**
     * 删除应急物资库存
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:stock:remove')")
    @Log(title = "应急物资库存", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stockIds}")
    public AjaxResult remove(@PathVariable Long[] stockIds) {
        return toAjax(emergencySuppliesStockService.deleteEmergencySuppliesStockByIds(stockIds));
    }
    
    /**
     * 物资入库
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:stock:in')")
    @Log(title = "应急物资入库", businessType = BusinessType.INSERT)
    @PostMapping("/in")
    public AjaxResult stockIn(@RequestBody EmergencySuppliesStock emergencySuppliesStock) {
        return toAjax(emergencySuppliesStockService.stockIn(emergencySuppliesStock));
    }
    
    /**
     * 物资出库
     */
    @PreAuthorize("@ss.hasPermi('estate:emergency:stock:out')")
    @Log(title = "应急物资出库", businessType = BusinessType.UPDATE)
    @PostMapping("/out")
    public AjaxResult stockOut(@RequestBody EmergencySuppliesStock emergencySuppliesStock) {
        return toAjax(emergencySuppliesStockService.stockOut(emergencySuppliesStock));
    }
    
    /**
     * 获取即将过期的物资
     */
    @GetMapping("/expiring/list")
    public AjaxResult getExpiringSupplies() {
        List<EmergencySuppliesStock> list = emergencySuppliesStockService.selectExpiringSupplies();
        return success(list);
    }
    
    /**
     * 获取物资库存分布情况
     */
    @GetMapping("/distribution/list")
    public AjaxResult getStockDistribution() {
        List<Map<String, Object>> distribution = emergencySuppliesStockService.selectStockDistribution();
        return success(distribution);
    }
} 